<template>
    <div class="componentContainer">
        <div class="searchContainer">
            <ComponentSearch />
        </div>
        <div>
            <el-table
                :data="componentDataList.dataList"
                style="width: 100%; margin-bottom: 20px; margin-top: 20px;"
                row-key="id"
                border
                :stripe="true"
                :show-overflow-tooltip="true"
                class="tableContainer"
                max-height="70vh"
                ref="containerRef"
            >
                <el-table-column prop="componentName" label="组件名称-英文" />
                <el-table-column prop="componentCnName" label="组件名称-中文" />
                <el-table-column prop="componentType" label="组件类型" />
                <el-table-column prop="componentTemplate" label="组件内容" />
                <el-table-column prop="componentData" label="组件数据" />
                <el-table-column prop="componentComputed" label="计算属性" />
                <el-table-column align="center" width="300">
                    <template #header>
                        <span>操 作</span>
                    </template>
                    <template #default="scope">
                        <el-button v-if="scope.row.tenantId !== 'base' || currentTenantId === 'ywu'" size="small" type="success" @click="handleUpdate(scope.$index, scope.row)">修 改</el-button>
                        <el-button v-if="(scope.row.tenantId !== 'base' || currentTenantId === 'ywu' )&& scope.row.componentType === 'CUSTOM'" size="small" type="warning" @click="handleEdit(scope.$index, scope.row)">编 辑</el-button>
                        <el-button v-if="scope.row.tenantId !== 'base' || currentTenantId === 'ywu'" type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删 除</el-button>
                        <el-button v-if="scope.row.tenantId !== 'base' || currentTenantId === 'ywu'" type="primary" size="small" @click="handleCopy(scope.$index, scope.row)">复 制</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                v-model:current-page="componentDataList.pageIndex"
                v-model:page-size="componentDataList.pageSize"
                :small="false"
                :background="true"
                layout="prev, pager, next, jumper, sizes, total"
                :total="componentDataList.recordCount"
                @change="changePage"
                :page-sizes="[10, 20, 30, 50, 100, 200, 400]"
                style="float: right"
            />
        </div>
    </div>
    <div>
        <ComponentUpdateDialog :title="title" :componentForm="componentForm" />
        <ComponentDeleteDialog :componentForm="componentForm" />
    </div>
</template>

<script>
import ComponentSearch from "@/components/componentManage/ComponentSearch";
import ComponentUpdateDialog from "@/components/componentManage/ComponentUpdateDialog";
import ComponentDeleteDialog from "@/components/componentManage/ComponentDeleteDialog";
import {createTag} from "@/utils/tagCreate";
import {componentInfoQueryNoContent} from "@/request/components";

export default {
    name: "ComponentManage",
    data() {
        return {
            componentForm: {

            },
            title: ""
        }
    },
    components: {
        ComponentSearch,
        ComponentUpdateDialog,
        ComponentDeleteDialog
    },
    methods: {
        handleUpdate(index, raw) {
            this.title = "修改组件"
            this.componentForm = {
                id: raw.id,
                componentName: raw.componentName,
                componentCnName: raw.componentCnName,
                componentType: raw.componentType,
                componentTemplate: raw.componentTemplate,
                componentData: raw.componentData,
                componentComputed: raw.componentComputed,
                componentJson: raw.componentJson
            }
            this.$store.state.componentUpdateDialogVisible = true;
        },
        handleDelete(index, raw) {
            this.componentForm = {
                id: raw.id,
                componentName: raw.componentName,
                componentCnName: raw.componentCnName,
                componentType: raw.componentType,
                componentTemplate: raw.componentTemplate,
                componentData: raw.componentData,
                componentComputed: raw.componentComputed,
                componentJson: raw.componentJson
            }
            this.$store.state.componentDeleteDialogVisible = true;
        },
        handleCopy(index, raw) {
            this.title = "复制组件"
            this.componentForm = {
                id: "",
                componentName: raw.componentName,
                componentCnName: raw.componentCnName,
                componentType: raw.componentType,
                componentTemplate: raw.componentTemplate,
                componentData: raw.componentData,
                componentComputed: raw.componentComputed,
                componentJson: raw.componentJson
            }
            this.$store.state.componentUpdateDialogVisible = true;
        },
        changePage(index, size) {
            this.componentSearchData.pageInfo = {
                pageIndex: index,
                pageSize: size
            };
            componentInfoQueryNoContent(this.componentSearchData).then(res => {
                if (res.code === '200') {
                    this.$store.state.componentDataList = res.data;
                }
            })
        },
        handleEdit(index, raw) {
            // 发送路由
            this.$router.push({path:'/ywu/lowcode/lowcodeBuilder',query: {id:raw.id}})
        }
    },
    computed: {
        componentDataList() {
            return this.$store.state.componentDataList;
        },
        componentSearchData() {
            return this.$store.state.componentSearchData;
        },
        currentTenantId() {
            return this.$store.state.userInfo.currentTenantInfo.tenantId
        }
    },
    mounted() {
        createTag(this.$route, this.$store);
    },
    created() {
        componentInfoQueryNoContent(this.componentSearchData).then(res => {
            this.$store.state.componentDataList = res.data;
        })
    }
}
</script>

<style scoped lang="less">
.componentContainer {
    width: 98%;
    height: 100%;
    margin: 0.2%;
    .searchContainer {
        width: 100%;
        margin-top: 1%;
    }
    .tableContainer {
        margin-left: 1%;
    }
    .insertContainer {
        margin-left: 1%;
        margin-top: 0.7%;
    }
}
</style>